<template>
  <div class="dw-full">
    <el-form ref="formRef" label-width="auto" :model="activeFormData" class="form">
      <slot :status="menuItem.state?.value"></slot>
    </el-form>
  </div>
</template>
<script setup lang="ts" generic="T extends IMasterEntity">
import { dwHttp } from '@/utils/http';
import { onMounted, ref, type PropType } from 'vue';
import type { IMasterEntity } from '@/types/entity';
import { dwDeepClone, template } from '@/utils/func';
import type { IMenuItem } from '@/types/menu-group';
import type { IResponseData, Method } from '@/types/http';
import type { IAddInfoHttpConfig, IGetInfoHttpConfig, IHttpConfig, IUpdateInfoHttpConfig } from '@/types/menu-datagrid-form';

defineOptions({
  name: 'DwMenuGroupForm',
});

const props = defineProps({
  activeId: {
    type: String,
    required: true,
  },
  initFormData: {
    type: Object,
  },
  beforeSave: {
    type: Function as PropType<(data: T, add?: boolean) => T | undefined>,
  },
  afterSuccessSave: {
    type: Function as PropType<(data: T, add?: boolean) => void>,
  },
  menuItem: {
    type: Object as PropType<IMenuItem>,
    required: true,
  },
  renderBindEditData: null, //渲染编辑的数据
  getInfoHttpConfig: {
    type: Object as PropType<IGetInfoHttpConfig<T>>,
  },
  addInfoHttpConfig: {
    type: Object as PropType<IAddInfoHttpConfig<T>>,
  },
  updateInfoHttpConfig: {
    type: Object as PropType<IUpdateInfoHttpConfig<T>>,
  },
});
const activeId = props.activeId;
const formRef = ref();

const activeFormData = defineModel<T>('activeFormData');
const initFormData: T = props.initFormData as unknown as T;
const formDataMap = defineModel<Record<string, T>>('formDataMap');

/**
 * 对formData数据格式进行处理
 */
const { getInfoHttpConfig, addInfoHttpConfig, updateInfoHttpConfig, beforeSave } = props;

const getInfo = (id: string) => {
  dwHttp({
    url: template(getInfoHttpConfig!.url, { id: id }),
    method: 'get',
    isSuccessTip: false,
  }).then((res) => {
    const { data } = res as unknown as IResponseData;
    let formData: T | undefined;
    if (typeof props.renderBindEditData === 'function') {
      formData = props.renderBindEditData(data) as unknown as T;
    } else {
      formData = data as unknown as T;
    }
    if (!formDataMap.value) {
      formDataMap.value = {};
    }
    formDataMap.value[activeId] = formData;
    activeFormData.value = dwDeepClone(formData as unknown as Record<string, unknown>) as T;
  });
};
const onSave = async (add?: boolean) => {
  await formRef.value.validate((valid: boolean) => {
    add = !!add;
    let method: Method = 'post';
    let saveHttpConfig: IHttpConfig | undefined;
    let result = activeFormData.value; // formData.value;
    if (typeof beforeSave === 'function') {
      result = beforeSave(activeFormData.value!, add);
    }
    if (props.menuItem.state?.value === 'edit') {
      method = 'put';
      saveHttpConfig = updateInfoHttpConfig;
    } else {
      saveHttpConfig = addInfoHttpConfig;
    }
    if (valid) {
      dwHttp({
        url: saveHttpConfig?.url,
        method: method,
        data: { ...result },
      }).then((res) => {
        if (typeof props.afterSuccessSave === 'function') {
          props.afterSuccessSave(res.data as unknown as T, add);
        }
      });
    }
  });
};

onMounted(() => {
  if (props.menuItem.state?.value === 'edit') {
    getInfo(props.menuItem.dataId!);
  } else {
    if (!formDataMap.value) {
      formDataMap.value = {};
    }
    const defaultFormData: T = dwDeepClone(initFormData as unknown as Record<string, unknown>) as unknown as T;
    formDataMap.value[activeId] = defaultFormData;
    activeFormData.value = dwDeepClone(defaultFormData as unknown as Record<string, unknown>) as T;
  }
});
const resetForm = () => {
  formRef.value.resetFields();
};
const getFormState = () => {
  return props.menuItem.state?.value;
};

defineExpose({ resetForm, onSave, getFormState });
</script>

<style lang="scss" scoped>
.dw-full {
  width: 100%;
  height: 100%;
}
.form-btn-warpper {
  height: 45px;
  display: flex;
  justify-items: center;
  align-items: center;
  vertical-align: middle;
  justify-content: end;

  border-bottom: 1px solid #dcdfe6;
}
.form {
  overflow: auto;
  height: calc(100% - 20px);
  padding: 10px;
}
</style>
<style lang="scss">
table {
  border-collapse: collapse;
  border-spacing: 0;
  :deep(.el-form-item__error) {
    z-index: 9999;
  }
}
.dw-form-table {
  border: solid 1px #e5e5e5;
  width: 100%;
}

.dw-form-table tr td {
  // line-height: 40px;
  border-right: solid 1px #e5e5e5;
  border-bottom: solid 1px #e5e5e5;
  padding: 5px 15px;
  text-indent: 0;

  .required::before {
    content: '*';
    color: red;
    padding-right: 10px;
    height: 24px;
    line-height: 28px;
  }
}
.dw-form-table td em {
  font-style: normal;
  color: #8d8d8d;
  font-size: 12px;
}

.dw-td-label {
  background: #f6f6f6;
  width: 123px;
  text-align: right;
  padding-right: 20px;
  white-space: nowrap;
}
</style>
